class Solution {
public:
    int minimumMountainRemovals(vector<int>& a) {
        int n = a.size();
        vector<int>f(n, 1), g(n);
        for (int i = 1; i < n - 1; i++) {
            //f[i]=1;
            for (int j = 0; j < i; j++) {
                if (a[j] < a[i])f[i] = max(f[j] + 1, f[i]);
            }
        }
        for (int i = n - 1; i >= 1; i--) {
            g[i] = 1;
            for (int j = n - 1; j > i; j--) {
                if (a[i] > a[j])g[i] = max(g[j] + 1, g[i]);
            }
        }
        int ma = 0;
        for (int i = 1; i < n - 1; i++) {
            if (f[i] > 1 && g[i] > 1)ma = max(f[i] + g[i] - 1, ma);
        }
        return n - ma;
    }
};